//
//  DebugMainSwitchView.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/15.
//

#import "DebugMainSwitchCell.h"
#import "DebugItemManager.h"

@interface DebugMainSwitchCell ()

///
@property(nonatomic,copy) BOOL(^switchViewBlock)(BOOL result);
///
@property (nonatomic,copy) NSString *selfUDName;
///
@property(nonatomic,strong) UILabel *titleLabel;
///
@property(nonatomic,strong) UISwitch *mainSwitch;
///
@property(nonatomic,strong) UIButton *mainButton;
///
@property(nonatomic,strong) NSIndexPath *currentIndex;

@end

@implementation DebugMainSwitchCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self bindData];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = UIColor.clearColor;
    [self addSubview:self.titleLabel];
    [self addSubview:self.mainSwitch];
    [self addSubview:self.mainButton];
}

- (void)bindData
{
    
}

- (void)setData:(NSIndexPath *)indexPath
{
    self.currentIndex = indexPath;
    __weak typeof(self) weakSelf = self;
    [[DebugItemManager sharedInstance] getSwitchItemDetail:indexPath detailBlock:^(NSString * _Nonnull switchItemName, NSString * _Nonnull switchItemNotificationName, BOOL (^ _Nonnull itemBlock)(BOOL)) {
        
        weakSelf.selfUDName = switchItemNotificationName;
        weakSelf.titleLabel.text = switchItemName;
        weakSelf.switchViewBlock = itemBlock;
        
        BOOL result = [[[NSUserDefaults standardUserDefaults] objectForKey:switchItemNotificationName]?:@"0" boolValue] ;
        [weakSelf.mainSwitch setOn:result animated:NO];
    }];
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, self.frame.size.width * 0.4, 30)];
        if (__builtin_available(iOS 13.0, *)) {
            _titleLabel.textColor = UIColor.labelColor;
        } else {
            _titleLabel.textColor = UIColor.blackColor;
        }
        _titleLabel.font = [UIFont systemFontOfSize:(self.frame.size.width * 0.096) weight:UIFontWeightBold];
        _titleLabel.textAlignment = NSTextAlignmentRight;
    }
    return _titleLabel;
}
- (UISwitch *)mainSwitch
{
    if (!_mainSwitch) {
        _mainSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.titleLabel.frame) + 5, 10, 0, 0)];
        CGFloat scale = (self.frame.size.height / self.frame.size.width)*2;
        _mainSwitch.transform = CGAffineTransformMakeScale(scale, scale);
    }
    return _mainSwitch;
}
- (UIButton *)mainButton
{
    if(!_mainButton)
    {
        _mainButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _mainButton.frame = self.mainSwitch.frame;
        [_mainButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mainButton;
}
- (void)buttonClick:(UIButton *)sender
{
    if (self.switchViewBlock) {
        
        BOOL result = [[[NSUserDefaults standardUserDefaults] objectForKey:self.selfUDName]?:@"0" boolValue] ;
        
        result = self.switchViewBlock(!result);
        
        [[NSUserDefaults standardUserDefaults] setObject:@(result) forKey:self.selfUDName];
        [self.mainSwitch setOn:result animated:YES];
    }
}
- (void)dealloc
{
    
}

@end
